package edu.upc.fib.lamsandroid.client.fragment.lesson;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import edu.upc.fib.lamsandroid.server.api.model.ToolType;

public class PlaceHolderLessonFragment extends Fragment {

    private final ToolType toolType;

    public PlaceHolderLessonFragment(ToolType toolType) {
        this.toolType = toolType;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Context c = getActivity().getApplicationContext();

        LinearLayout layout = new LinearLayout(c);
        LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.MATCH_PARENT, 0);

        layout.setLayoutParams(params);

        WebView webView = new WebView(layout.getContext());
        webView.getSettings().setPluginState(WebSettings.PluginState.ON);
        webView.getSettings().setJavaScriptEnabled(true);

        // open all links with external browser
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(intent);
                return true;
            }
        });

        webView.loadData("Tool content type renderer still not implemented: ID="+toolType.getExternalId(), "text/html", "UTF-8");
        layout.addView(webView);

        ScrollView scrollView = new ScrollView(getActivity());
        scrollView.addView(layout);
        return scrollView;
    }
}
